$().ready(function() {
	validateRule();
	var canNext = isCompanyInfo();
	if(!canNext){
		return false;
	}
	var industrys;
	var jobPosition;
	$.ajax({
		cache : true,
		type : "GET",
		url : "/front/jobPosition/getAll",
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			jobPosition = data.data;
			$.each(jobPosition,function(index,item){
				$('#jobPositionP').append("<option value="+item.id+">"+item.positionName+"</option>");
			});

			$('#jobPositionP').change(function(){
				var value = $(this).val();
				$.each(jobPosition,function(index,item){
					if(item.id == value/1){
						$('#jobPosition').empty();
						$.each(item.nodes,function(index,jobPositionItem){
							$('#jobPosition').append("<option value="+jobPositionItem.id+">"+jobPositionItem.positionName+"</option>");
						});
					}
				});
			});
			$('#jobPositionP').change();
		}
	});

	$.ajax({
		cache : true,
		type : "GET",
		url : "/system/industry/getAll",
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			industrys = data.data;
			$.each(industrys,function(index,item){
				$('#industryNoP').append("<option value="+item.id+">"+item.industryName+"</option>");
			});

			$('#industryNoP').change(function(){
				var value = $(this).val();
				$.each(industrys,function(index,item){
					if(item.id == value/1){
						$('#industryNo').empty();
						$.each(item.nodes,function(index,industryItem){
							$('#industryNo').append("<option value="+industryItem.id+">"+industryItem.industryName+"</option>");
						});
					}
				});
			});
			$('#industryNoP').change();
		}
	});
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
function save() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/company/post/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);
			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}

function isCompanyInfo(){
	var isCompanyInfo = false;
	layui.use("layer",function(){
		var layer = layui.layer;  //layer初始化
		$.ajax({
			url : "/company/post/isCompanyInfo",
			type : "GET",
			async : false,
			data : '',
			success : function(r) {
				if (r.code == 0) {
					isCompanyInfo = r.data.isCompanyInfo;
					if(!isCompanyInfo){
						parent.layer.msg("请先完善[企业基本信息]后再发布岗位！");
						var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
						parent.layer.close(index);
					}
				} else {
					layer.msg(r.msg);
				}
			}
		});
	})
	return isCompanyInfo;
}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入姓名"
			}
		}
	})
}